﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using OWL.Rendering.HRP;
using UnityEngine;
using Yoozoo.Mars.Got;

namespace Yoozoo.Core.Common
{
    public class RenderComponentQualityListener : QualityListenerMono,
        IPostEffectAdjuster
    {
        [Serializable]
        public class RenderComponentStyleSetting
        {
            public int style = 0;
            
            public Transform ComponentRoot;
            [DisplayOnly]
            public List<HSceneRenderSettingsComponent> RenderSettingsComponents;
            
            public void Enable()
            {
                if (RenderSettingsComponents!=null)
                {
                    foreach (var component in RenderSettingsComponents)
                    {
                        if (!component.enabled)
                        {
                            component.enabled = true;
                        }
                    }
                }
            }

            public void Disable()
            {
                if (RenderSettingsComponents!=null)
                {
                    foreach (var component in RenderSettingsComponents)
                    {
                        if (component.enabled)
                        {
                            component.enabled = false;
                        }
                    }
                }

                if (ComponentRoot && !ComponentRoot.gameObject.activeSelf)
                {
                    ComponentRoot.gameObject.SetActive(true);
                }
            }

            public void GetRenderComponents()
            {
                if (ComponentRoot)
                {
                    RenderSettingsComponents = ComponentRoot.GetComponents<HSceneRenderSettingsComponent>().ToList();
                }
            }
        }


        public List<RenderComponentStyleSetting> styleSettings;

        protected void Awake()
        {
            foreach (var setting in styleSettings)
            {
                setting.Disable();
            }
        }
        
        
        public void ChangePostEffectStyle(int style)
        {
            foreach (var setting in styleSettings)
            {
                setting.Disable();
            }

            RenderComponentStyleSetting styleSetting = null;
            foreach (var setting in styleSettings)
            {
                if (setting.style == style)
                {
                    styleSetting = setting;
                    break;
                }
            }

            if (styleSetting != null)
            {
                //Timers.inst.CallLater((o) =>
                //{
                    styleSetting.Enable();
                /*if (!hasLateUpdated)
                {
                    needUpdateStyleSetting = styleSetting;
                    needLateUpdate = true;
                }*/
                //});
            }
        }

        private bool needLateUpdate;
        private RenderComponentStyleSetting needUpdateStyleSetting;

        private bool hasLateUpdated = false;
        
        /*private void Update()
        {
            hasLateUpdated = false;
        }
        
        private void LateUpdate()
        {
            hasLateUpdated = true;
            if (needLateUpdate)
            {
                if (needUpdateStyleSetting != null)
                {
                    needUpdateStyleSetting.Disable();
                    needUpdateStyleSetting.Enable();
                    needUpdateStyleSetting = null;
                }
                needLateUpdate = false;
            }
        }*/

        public void ChangePostEffectLevel(int level)
        {
            
        }


#if UNITY_EDITOR

        private void OnValidate()
        {
            if (styleSettings!=null)
            {
                foreach (var setting in styleSettings)
                {
                    setting.GetRenderComponents();
                }
            }
        }
#endif

    }
}